package com.fplugin.ushare;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;


public class Umengshare {
  private  Activity activity;
  private  Context context;
  private SHARE_MEDIA[] sharelist = {SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,
            SHARE_MEDIA.SINA};

    Umengshare(Activity at, Context ct) {
        context = ct;
        activity = at;
    }

    void shareUrl(@NonNull MethodCall call, @NonNull final MethodChannel.Result result){
        String title = call.argument("title");
        String desc = call.argument("desc");
        String url = call.argument("url");
        String imageUrl = call.argument("imageUrl");
        int platform = call.argument("platform");

        UMWeb web = new UMWeb(url);
        UMImage image = new UMImage(activity, imageUrl);//网络图片
        web.setTitle(title);//标题
        web.setThumb(image);  //缩略图
        web.setDescription(desc);//描述
        new ShareAction(activity)
            .setPlatform(sharelist[platform])//传入平台
            .withMedia(web)
            .setCallback(shareListener)//回调监听器
            .share();
    }

    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            Toast.makeText(activity,"分享开始",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(activity,"成功了",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(activity,"失败"+t.getMessage(),Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(activity,"取消了",Toast.LENGTH_LONG).show();
        }
    };
}
